﻿using Aop.Api;
using Aop.Api.Domain;
using Aop.Api.Request;
using Aop.Api.Response;
using BLL.sysmanager;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.paymentmanager;


public partial class paymentmanager_zfb_lis : System.Web.UI.Page
{

    private ps_users ps_Users = new ps_users();
    PaymentService bll = new PaymentService();


    protected void Page_Load(object sender, EventArgs e)
    {
        string strSql = "SELECT id,user_name FROM ps_users";
        DataSet ds = ps_Users.GetListSql(strSql);
        foreach(DataRow dr in ds.Tables[0].Rows)
        {
            string Id = dr["id"].ToString();
            string Title = dr["user_name"].ToString().Trim();
            DropDownList1.Items.Add(new ListItem(Title, Id));
        }

    }

    protected void BtnAlipay_Click(object sender, EventArgs e)
    {
        DefaultAopClient client = new DefaultAopClient(Config.gatewayUrl, Config.app_id, Config.private_key, "json", "1.0", Config.sign_type, Config.alipay_public_key, Config.charset, false);

        // 外部订单号，商户网站订单系统中唯一的订单号
        string out_trade_no = WIDout_trade_no.Text.Trim();

        // 订单名称
        string subject = WIDsubject.Text.Trim();

        // 付款金额
        string total_amount = WIDtotal_amount.Text.Trim();

        // 商品描述
        string body = WIDbody.Text.Trim();

        // 组装业务参数model
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.Body = body;
        model.Subject = subject;
        model.TotalAmount = total_amount;
        model.OutTradeNo = out_trade_no;
        model.ProductCode = "FAST_INSTANT_TRADE_PAY";

        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        // 设置同步回调地址
        request.SetReturnUrl("http://localhost:22865/main.aspx");
        // 设置异步通知接收地址
        request.SetNotifyUrl("");
        // 将业务model载入到request
        request.SetBizModel(model);

        AlipayTradePagePayResponse response = null;
        try
        {
            response = client.pageExecute(request, null, "post");

            Response.Write(response.Body);
            int id = int.Parse(DropDownList1.SelectedValue.ToString());
            int pointCount = int.Parse(WIDtotal_amount.Text.Trim());
            ps_Users.CheakPoint(id, pointCount);
            ps_play_log play_Log = new ps_play_log()
            {
                play_id = out_trade_no,
                play_count = int.Parse(total_amount),
                remark = body,
                Merchant_Id = Config.app_id,
                byuser_Id = "cunnyl0509@sandbox.com",
                Means_payment = "支付宝",
                play_username = DropDownList1.SelectedItem.Text.Trim(),
                play_name = subject,
                play_userId = int.Parse(DropDownList1.SelectedValue.Trim())
            };
            PlayServer server = new PlayServer();
            server.AddNewPlayLog(play_Log);
        }
        catch (Exception exp)
        {
            throw exp;
        }
    }
}
